package pos.data;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import pos.utils.*;

import pos.domain.Property;
import pos.domain.Service;
import pos.domain.User;

public class JDBCUserDAO implements IUserDAO{
	
	private IPropertyDAO pdao=new JDBCPropertyDAO();
	private IServiceDAO sdao=new JDBCServiceDAO();
	//Connection conn=ConnectionManager.getInstance().checkOut(); 
	
	 public void delete(String userOID) {
		  Connection conn=ConnectionManager.getInstance().checkOut();
	        String sql = "DELETE FROM User WHERE (userOID = ?) ";
	        PreparedStatement stmt = null;

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, userOID);
	            stmt.executeUpdate();
	        } catch (SQLException e) {
	            System.err.println("Message: " + e.getMessage());
	            System.err.println("SQLState: " + e.getSQLState());
	            System.err.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	        	ConnectionManager.getInstance().checkIn(conn);
	            try {
	                if (stmt != null)
	                    stmt.close();
	            } catch (SQLException e) {
	            }
	        }
	    }

	 
	public void insertUser(User u,Property p,Service s) {
		
		  String sql = "INSERT INTO User ( userOID,password, name, surname, DNI, phone, email, birthdate,calification) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ";
		  Connection conn=ConnectionManager.getInstance().checkOut(); 
		  PreparedStatement stmt = null;
	        UIDGenerator userOID=UIDGenerator.getInstance();
	        try {
	        	
	        	 stmt = conn.prepareStatement(sql);
	        	 
	        	stmt.setString(1, userOID.getKey());	           
	            stmt.setString(2, u.getPassword());	           
	            stmt.setString(3, u.getName());
	            stmt.setString(4, u.getSurname());
	            stmt.setString(5, u.getDNI());
	            stmt.setString(6, u.getPhone());
	            stmt.setString(7, u.getEmail());
	            stmt.setString(8, u.getBirthDay());
	            stmt.setDouble(9, 0.0f);

	            System.out.println("User dentro de JDBC:");
	            System.out.println(userOID.getKey());	           
	            System.out.println(u.getPassword());	           
	            System.out.println( u.getName());
	            System.out.println( u.getSurname());
	            System.out.println( u.getDNI());
	            System.out.println( u.getPhone());
	            System.out.println( u.getEmail());
	            System.out.println( u.getBirthDay());
	            System.out.println( 0.0f);
	            
	            int i=stmt.executeUpdate();
	           // System.out.println("rows en user:"+i);
	            //System.out.println("property:"+p.getCountry());
		        pdao.insert(conn,u.getEmail(),p,s);
		        
	           
	            
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	        	ConnectionManager.getInstance().checkIn(conn);

	            try {
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }
		
	       
	}

	public User select( String userOID) {
		
		Connection conn=ConnectionManager.getInstance().checkOut();
		 String sql = "SELECT * FROM User WHERE (OID = ? ) ";
	        PreparedStatement stmt = null;
	        ResultSet result = null;
	       User u = null;

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, userOID);

	            result = stmt.executeQuery();

	            result.next();
	            u = new User();
	            u.setPassword(result.getString("password"));
	            u.setName(result.getString("name"));
	            u.setSurname(result.getString("surname"));
	            u.setDNI(result.getString("DNI"));
	            u.setBirthDay(result.getString("birthday"));
	            u.setEmail(result.getString("email"));
	            u.setPhone(result.getString("phone"));
	            float cal=Float.parseFloat(result.getString("calification"));
	            u.setCalification(cal);
	            
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	        	ConnectionManager.getInstance().checkIn(conn);

	            try {
	            	ConnectionManager.getInstance().checkIn(conn);
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }
	       

	        return u;
	    }
	/* en principio no me sirve
	public float selectUserCalification(String dni) {
			Connection conn=ConnectionManager.getInstance().checkOut();
			String sql = "SELECT * FROM User WHERE (dni = ? ) ";
	        PreparedStatement stmt = null;
	        ResultSet result = null;
	        float calification=0;
	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, dni);

	            result = stmt.executeQuery();

	            result.next();
	           
	            calification=result.getFloat("calification");
	            
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }

	        return calification ;
	    }
	
	*/
	public User selectUser(String email){
		 Connection conn=ConnectionManager.getInstance().checkOut();
	        String sql = "SELECT * FROM User WHERE (email = ?) ";
	        PreparedStatement stmt = null;
	        ResultSet result = null;
		    User u=null;   

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, email);
	            result=stmt.executeQuery();
	            
	            if(result.next()){
	            	u=new User();
	            	//u.setUserOID(result.getString("userOID"));
	            	u.setEmail(result.getString("email"));
	            	u.setName(result.getString("name"));
	            	u.setSurname(result.getString("surname"));
	            	u.setEmail(result.getString("email"));
	            	u.setBirthDay(result.getString("birthDate"));
	            	u.setDNI(result.getString("DNI"));
	            	u.setPhone(result.getString("phone"));
	            	u.setPassword(result.getString("password"));
	            	u.setCalification(result.getFloat("calification"));
	            }
	            
	        } catch (SQLException e) {
	            System.err.println("Message: " + e.getMessage());
	            System.err.println("SQLState: " + e.getSQLState());
	            System.err.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	        	ConnectionManager.getInstance().checkIn(conn);

	            try {
	                if (stmt != null)
	                    stmt.close();
	            } catch (SQLException e) {
	            }
	        }
	        return u;
	    }

	public void deleteUser(String userOID) {
		// TODO Auto-generated method stub
		
	}

	public void insertUser(String userOID, User u) {
		// TODO Auto-generated method stub
		
	}

	public List<User> selectAllUsers() {
		// TODO Auto-generated method stub
		return null;
	}
	public String selectOID(Connection conn,String email){
		 PreparedStatement stmt = null;
	        ResultSet result = null;
	        String oidu = null;
	        String sql = "SELECT * FROM User WHERE (email = ?) ";

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, email);
	            result = stmt.executeQuery();

	            result.next();
	            oidu = result.getString("userOID");
	            
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }
	        return oidu;
	}


		
		
	}



